package it.polimi.swimv2.session;

import it.polimi.swimv2.entities.User;
import it.polimi.swimv2.remote.UserSearchManagerRemote;

import java.util.Collection;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class UserSearchManager
 */
@Stateless
public class UserSearchManager implements UserSearchManagerRemote {
	@PersistenceContext(unitName = "swimv2")
	private EntityManager em;

	/**
	 * Default constructor.
	 */
	public UserSearchManager() {
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<User> userSearch(String username, String firstname,
			String surname, String city) {
		Query q = em.createQuery(
				"SELECT OBJECT (u) FROM User u " +
				"WHERE u.username LIKE '"+ username+ "%' "
						+"AND u.firstname LIKE '"+ firstname+ "%' " 
						+"AND u.surname LIKE '" + surname + "%' "
						+"AND u.city LIKE '"+city+"%'");
		Collection<User> result = (Collection<User>) q.getResultList();
		return result;

	}

}
